package com.example.fengyungo.utils;

import static android.content.Context.MODE_PRIVATE;

import android.app.Service;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;

import androidx.fragment.app.FragmentActivity;

import com.example.fengyungo.constant.DriverConstant;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class BitmapUtils {

    private static final String TAG = "BitmapUtils";

    /**
     * 将字节图片转为图片
     *
     * @param bytes byte[]
     * @param opts  BitmapFactory.Options
     * @return Bitmap
     */
    public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) {
        Log.d("lyfsInBitmap", "begin");
        if (bytes != null) {
            if (opts != null) {
                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
            } else {
                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            }
        } else {
            Log.d("lyfsInBitmap", "bad");
        }
        return null;
    }

    /**
     * 在服务中将Bitmap转换成字符串保存至SharedPreferences，name为"image"
     *
     * @param bitmap  Bitmap
     * @param service Service
     */
    public static void saveBitmapToSharedPreferences(Bitmap bitmap, Service service, String key) {
        // 第一步:将Bitmap压缩至字节数组输出流ByteArrayOutputStream
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
        // 第二步:利用Base64将字节数组输出流中的数据转换成字符串String
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        String image = Base64.encodeToString(byteArray,
                Base64.DEFAULT);
        // 第三步:将String保持至SharedPreferences
        SharedPreferences spf = service.getSharedPreferences(DriverConstant.SP_NAME_IMAGE, MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();
        editor.putString(key, image);
        editor.apply();
    }

    /**
     * 在FragmentActivity中将Bitmap转换成字符串保存至SharedPreferences
     *
     * @param bitmap  Bitmap
     * @param fragmentActivity FragmentActivity
     */
    public static void saveBitmapToSharedPreferences(Bitmap bitmap, FragmentActivity fragmentActivity, String key) {
        // 第一步:将Bitmap压缩至字节数组输出流ByteArrayOutputStream
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
        // 第二步:利用Base64将字节数组输出流中的数据转换成字符串String
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        String image = Base64.encodeToString(byteArray,
                Base64.DEFAULT);
        // 第三步:将String保持至SharedPreferences
        SharedPreferences spf = fragmentActivity.getSharedPreferences(DriverConstant.SP_NAME_IMAGE, MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();
        editor.putString(key, image);
        editor.apply();
    }

    /**
     * 在FragmentActivity中从SharedPreferences中取出Bitmap，显示在ImageView中
     *
     * @param fragmentActivity FragmentActivity
     * @return Bitmap
     */
    public static Bitmap getBitmapFromSharedPreferences(FragmentActivity fragmentActivity, String key) {
        SharedPreferences spf = fragmentActivity.getSharedPreferences(DriverConstant.SP_NAME_IMAGE, MODE_PRIVATE);
        // 第一步:取出字符串形式的Bitmap
        String image = spf.getString(key, "");
//        Log.d(TAG, "getBitmapFromSharedPreferences: image(string)="+image);
        // 第二步:利用Base64将字符串转换为ByteArrayInputStream
        byte[] byteArray = Base64.decode(image, Base64.DEFAULT);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                byteArray);
        // 第三步:利用ByteArrayInputStream生成Bitmap
        return BitmapFactory.decodeStream(byteArrayInputStream);
    }


}
